/**
* jPanels - Multiple Panels View for jQuery  
* Released under GPL v3 License
* @author Cristian Ariel Cortez  
* @copyright (c) 2010 - 2012 Cristian Ariel Cortez - cortez[dot]cristian[at]gmail[dot]com - http://skyconcept.com.ar/crisboot/
* @date 25/01/2011
* @requires jQuery v1.4 or above
*
*/

/**
*  HTML markup example:

*
*/

;(function($){
	$.fn.jpanels = function(obj){
		/**
		* Configuration Object
		*/
		var o = $.extend({
			beforeStart: function(instObject){
				//use the entire window
				instObject.height($(window).height()).css('overflow','hidden');
			},
			afterEnd: function(instObject){
				if(o.accessibilityEnabled)
					o.handleAccessibility(instObject);
			},
			beforeResize: function(instObject){
				//use the entire window
				instObject.height($(window).height());
			},
			afterResize: function(instObject){ 
				
			},
			handleAccessibility: function(instObject){
				var isCtrl = false, allCollpased=true;

				$(document).keyup(function (e) {
					if(e.which == 17) isCtrl=false;
				}).keydown(function (e) {
					if(e.which == 17) isCtrl=true;
					
					if(e.which == 77 && isCtrl == true) {//33- m
						//run code for CTRL+S -- ie, save!
						
						debuger("control + M");
						
						$.each(o.panelNames, function(i, el){
							//debuger(o.state[i] + (o.state[i]==allCollpased));
							if(o.state[i]==allCollpased)
								instObject.find(o.panelNames[i] +' .panel-header').click();
								
							
						});
						
						allCollpased = !allCollpased;
						
						//debuger(allCollpased);
						//instObject.find(o.panelNames[i] +' .panel-header').click();
						
						
						return false;
					}
				});
				
			},
			accessibilityEnabled: true,
			debug: true,
			debugColors: false,
			panelNames: {
				north:".p-north", east:".p-east", south:".p-south", west:".p-west", center:".p-center"
			},
			panelHeight: {north:200, south:200},
			panelWidth: {east:250, west:250},
			panelHeader: {
				north:"North Panel", east:"East Panel", south:"South Panel", west:"West Panel", center:"Center Panel"
			},
			panelContent: {
				north:"", east:"", south:"", west:"", center:""
			},
			/**
			*	State: 1 - Expanded, 0 - Collapsed
			*	3 - The panel does not exists
			*/
			state: {
				north:1, east:1, south:1, west:1, center:1
			}
		}, obj || {});
		
		/**
		* Function Debuger to show logs in Firebug
		*/
		function debuger(){
			if(o.debug)
				try{console.log.apply('',arguments);} catch(e) {}
		}

		function isset(instObject, panelName){
			if(instObject.find(panelName))
				return true;
			else
				return false;	
		}

		var pMixin = {
			centerHeight: function(instObject){			
				return instObject.height() - instObject.find(o.panelNames.north).height() - instObject.find(o.panelNames.south).height()-6;
			},
			centerWidth: function(instObject, diff){
				/**
				* East makes bigger and the scrollbar appears
				* it's not givin time for the center to be smaller
				*/
				var d = diff || (-6);//6px due border
				return instObject.width() - instObject.find(o.panelNames.west).width() - instObject.find(o.panelNames.east).width()+d;//6px due borders
			},
			resizeVertically: function(instObject){
				var h = pMixin.centerHeight(instObject);
				instObject.find(o.panelNames.east).css('height', h+'px');
				instObject.find(o.panelNames.center).css('height', h+'px');
				instObject.find(o.panelNames.west).css('height', h+'px');
				if(o.state.east==0)
					instObject.find(o.panelNames.east+' .panel-header').css('height', h+'px');
				if(o.state.west==0)
					instObject.find(o.panelNames.west+' .panel-header').css('height', h+'px');
				pMixin.center(instObject);				
			},
			resizeHorizontally: function(instObject){
				var w = pMixin.centerWidth(instObject),
				east = instObject.find(o.panelNames.east).width(), west = instObject.find(o.panelNames.west).width();
				instObject.find(o.panelNames.south).css('width', instObject.width());
				//if(w>(east+west+6))
				//	$('.p-center').css('width', w+'px');
				pMixin.center(instObject);
				//$('.p-center .panel-content').html(cmdshell.mod['panel'].logPanels());
			},
			resizeAllPanels: function(instObject){
				debuger('resize all panels');
				pMixin.resizeVertically(instObject);
				pMixin.resizeHorizontally(instObject);
			},
			north : function(instObject){
				debuger('North Init');
				instObject.find(o.panelNames.north+' .panel-header').toggle( function(){
					$(this).next().hide();	
					//save status
					o.state.north = 0;
					pMixin.resizeVertically(instObject);
			
				}, function(){
					$(this).next().show();			
					//save status
					o.state.north = 1;
					pMixin.resizeVertically(instObject);
				});	
			},
			east:function(instObject){
				debuger("East Init");
				var w = o.panelWidth.east || 250;
				var h = pMixin.centerHeight(instObject);
						
				instObject.find(o.panelNames.east).css('height', h+'px');
				instObject.find(o.panelNames.east+' .panel-header').bind('click', function(){
					
					if(o.state.east==1){//it's expanded
						//save header
						o.panelHeader.east = $(this).html();
						
						//collapse it please
						$(this).next().hide();	
						$(this).parent().css('width','20px');				
						$(this).css('width','20px').css('height',instObject.find(o.panelNames.east).height()+'px').html('');
						
						//save status
						o.state.east = 0;
						pMixin.center(instObject);
						
					}else if(o.state.east==0){//it's collapsed
						//expand it please
						$(this).parent().css('width',w+'px');
						$(this).css('width',w).css('height','15px');
						$(this).html(o.panelHeader.east).next().show();
						
						//save status
						o.state.east = 1;
						pMixin.center(instObject);//14px due scroll bar
					}
					
				});
				
			}, 
			south:function(instObject){
				debuger("South Init");
				var h = o.panelHeight.south || 200;
				var w = instObject.width();
				instObject.find(o.panelNames.south).css('width', w+'px');
				//$(this).next().css('height',h+'px');
				instObject.find(o.panelNames.south+' .panel-header').toggle( function(){
					$(this).next().hide();
					//save status
					o.state.south = 0;
					pMixin.resizeVertically(instObject);
				}, function(){
					$(this).next().show();
					//save status
					o.state.south = 1;
					pMixin.resizeVertically(instObject);
				});
			}, 
			west:function(instObject){
				debuger("west")
				var w = o.panelWidth.west || 250;
				var h = pMixin.centerHeight(instObject);
				
				instObject.find(o.panelNames.west).css('height', h +'px');
				instObject.find(o.panelNames.west+' .panel-header').bind('click', function(){
					
					if(o.state.west==1){//it's expanded
						//save header
						o.panelHeader.west = $(this).html();
						//collapse it please
						$(this).next().hide();	
						$(this).parent().css('width','20px');				
						$(this).css('width','20px').css('height',instObject.find(o.panelNames.west).height()+'px').html('');
						
						//save status
						o.state.west = 0;
						pMixin.center(instObject);

						
					}else if(o.state.west==0){//it's collapsed
						//expand it please
						$(this).parent().css('width',w+'px');
						$(this).css('width',w).css('height','15px');
						$(this).html(o.panelHeader.west).next().show();
						
						//save status
						o.state.west = 1;
						pMixin.center(instObject);
					}
					
				});
			}, 
			center:function(instObject, diff){
				debuger("center");
				var h = pMixin.centerHeight(instObject), d = diff || (-6),//6px due borders
				w = pMixin.centerWidth(instObject, d);
				instObject.find(o.panelNames.center).css('height', h+'px').css('width', w+'px');
			}

		};
		
		function initNorthPanel(instObject){			
			instObject.find(o.panelNames.north+' .panel-header').toggle( function(){
				$(this).next().hide();	
				//save status
				
			}, function(){
				$(this).next().show();			
				//save status
			});	
		}

		function initPanels(instObject){
			initNorthPanel(instObject)
		}
		
		return this.each(function() {
			//console.log($(this));
		
			var curIns = $(this); //current Instance
			o.beforeStart(curIns);
			
			/**
			* Confirm panel existence
			*/			
			$.each(o.panelNames, function(i, el){
				//debuger(pMixin.north);
				if(!isset(curIns,el))
					o.state[i]=3
				else
					pMixin[i].apply(this, [curIns])
			});
			
			//initPanels(curIns);
			
			$(window).resize(function() {
				//debuger(curIns)
				o.beforeResize(curIns);
				pMixin.resizeAllPanels(curIns);
				o.afterResize(curIns);	
			});
		
			o.afterEnd(curIns);
		});//close return
	}
})(jQuery);


